﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using com.echonest.api.v4;
using jENIKVMApi;
using Double = java.lang.Double;

namespace RequestEchoNest.Data
{
    internal class MusicCatalog
    {
        private EchoNestAPI _api;

        public MusicCatalog(string developerKey)
        {
            _api = new EchoNestAPI(developerKey);
        }


        public void LoadSimilarArtists(string artistName)
        {
            try
            {
                var artistParams = new ArtistParams();
                artistParams.addName(artistName);
                _api.GetSimilarArtistsAsync(artistParams, (result) =>
                                                                  {
                                                                      var scoredArtists = (List<Artist>)result.AsyncState;
                                                                      if (LoadingSimilarArtistsCompleted != null)
                                                                      {
                                                                          var eventArgs = new ArtistsLoadedEventArgs(null, scoredArtists.Select(item => new ArtistModel(item)).ToList());
                                                                          LoadingSimilarArtistsCompleted(this, eventArgs);
                                                                      }

                                                                  });
            }
            catch (Exception ex)
            {
                if (LoadingSimilarArtistsCompleted != null)
                {
                    LoadingSimilarArtistsCompleted(this, new ArtistsLoadedEventArgs(ex, null));
                }
            }
        }

        public void LoadSimilarSongsForTrack(java.io.File trackFile)
        {
            ThreadPool.QueueUserWorkItem((state) =>
            {
                try
                {
                    Track track = _api.uploadTrack(trackFile, true);
                    var status = track.waitForAnalysis(180000);

                    if (status == Track.AnalysisStatus.COMPLETE)
                    {
                        var p = SimilarSongSpecification.Create(track);
                        // fails...
                        //List<Song> songs = _api.SimilarSongs(p);
                        List<Song> songs = _api.SearchSongs(p);

                        if (LoadingSimilarTracksCompleted != null)
                        {
                            LoadingSimilarTracksCompleted(this,
                                                          new SimilarTracksEventArgs { Similars = songs.Select(x => x.getArtistName() + " - " + x.getTitle()).ToArray() });
                        }

                    }
                }
                catch (Exception ex)
                {
                    LoadingSimilarTracksCompleted(this, new SimilarTracksEventArgs { Error = ex });
                }
            });
        }




        public event EventHandler<ArtistsLoadedEventArgs> LoadingSimilarArtistsCompleted;
        public event EventHandler<SimilarTracksEventArgs> LoadingSimilarTracksCompleted;

    }
}
